#!/usr/bin/env python

#    Copyright 2011,2012 Erik Persson
#
#    This file is part of the cell-sync-usrp project.
#
#    cell-sync-usrp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    cell-sync-usrp is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cell-sync-usrp.  If not, see <http://www.gnu.org/licenses/>.



"""
i = 0       # primary scrambling code number 0-511
n = 16*i

j = 0       # scrambling code group 0-63
k = 0       # scrambling code number 0-7
n = 16*8*j+16*k
    
x = 1 + X**7 + X**17
y = 1 + X**5 + X**7 + X**10 + X**18"""
from wx import py

startX = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
startY = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
xdict = {}
ydict = {}

def nextX(prevX):
    ret = []
    for i in range(0,18):
        ret.append((prevX[7+i]+prev[i])%2)
    return ret

def nextY(prevY):
    ret = []
    for i in range(0,18):
        ret.append((prevX[10+i]+prevX[7+i]+prevX[5+i]+prevX[i])%2)
    return ret

def getX(i):
    if xdict.has_key(i):
        return xdict[i]
    else:
        if (i<18):
            xdict[i] = startX[i]
            return startX[i]
        else:
            value = (getX(i+7-18) + getX(i-18)) % 2
            xdict[i] = value
            return value
    
def getY(i):
    if ydict.has_key(i):
        return ydict[i]
    else:
        if (i<18):
            ydict[i] = startY[i]
            return startY[i]
        else:
            value = (getY(i+10-18) + getY(i+7-18) + getY(i+5-18) + getY(i-18)) % 2
            ydict[i] = value
            return value
        
    
def getZ(n,i):
    value = (getX( (i+n) % (2**18 -1)) + getY(i)) % 2
    return value

import sys
sys.setrecursionlimit(50000)

"""print getZ(0,1)
print getZ(0,131072+1)"""

s = 16*8*12+16*7
b = 18*2
for i in range(s,s+b):
    real = getZ(s,(i-s))
    imag = getZ(s,(i-s)+131072)
    if (real==0):
        real=1
    else: 
        real = -1
    if (imag==0):
        imag = 1
    else:
        imag = -1
    #print (real+imag*-1j) ,
    if (i-s==18):
        print ""
    print ("("+str(real)+","+str(-imag)+"),"),


"""
s = 16*8*12+16*7
b = 384000
f = open("scramblingseqtest.h","w")
f.write("gr_complex stest[]={")
for i in range(s,s+b):
    real = getZ(s,i)
    imag = getZ(s,i+131072)
    if (real==0):
        real=1
    else: 
        real = -1
    if (imag==0):
        imag = 1
    else:
        imag = -1
    #print (real+imag*-1j) ,
    if (i!=s+b-1):
        f.write("gr_complex("+str(real)+","+str(-imag)+"),")
    else:
        f.write("gr_complex("+str(real)+","+str(-imag)+")")
            
f.write("};")
print "!"
"""


"""
print "Generating......"
f = open("scramblingseq.h","w")
f.write("const unsigned char scrambling_codes[][36][4] = {\n")
for j in range(0,64):
    for k in range(0,8):
        f.write("{")
        for i in range(0,18):
            value = "{"+str(getX(16*8*j+16*k+i)) + ", "
            value +=    str(getY(i)) + ", "
            value +=    str(getX(16*8*j+16*k+i+131072)) + ", "
            value +=    str(getY(i+131072)) + "}"
            #value = "0x" + str( getZ(16*8*j+16*k,i)) + str(getZ((16*8*j+16*k+131072)%(2**18-1),i))
            f.write(value)
            f.write(", ")
            f.write(value)
            if (i!=17):
                f.write(", ")
            
        if (k!=7):
            f.write("},\n")
        else:
            f.write("}\n")
    if (j!=63):
        f.write(",")
f.write("};")
print "Done!"
"""
tmpcnt = 0
cmd = raw_input("Generate test data (y/n):")
if cmd=='y':
    import struct
    print "Generating file 'p_cpichseq'....",
    f = open("p_cpichseq","wb")
    scramblingCode = []
    
    for i in range(0, 38400):
        real = getZ(16*8*12+16*7,i)
        imag = getZ(16*8*12+16*7,(i)+131072)
        if (real==0):
            real=1
        else: 
            real = -1
        if (imag==0):
            imag = 1
        else:
            imag = -1
        cmplx = complex(real,imag)
        scramblingCode.append(cmplx)
#        if (tmpcnt/2==0):
#            z = 1+1j
#        else:
#            z = -1-1j
       
        v = cmplx * (1+1j)# *(1+1j) * (1+1j);
       
       ## temporary
        z=getZ(16*8*12+16*7,i)
       # v = z*(1+1j)
       
        tmp = struct.pack("ff",v.real,v.imag)
        f.write(tmp)
        f.write(tmp)
        tmpcnt += 1
        if (tmpcnt==4):
            tmpcnt=0
    print "Done!"

